import time

import allure
import yaml
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from wecom_practice.base.base_page import BasePage
from wecom_practice.page.contact_page import ContactPage


class MainPage(BasePage):
    #通讯录按钮
    __CONTACT_BTN=By.XPATH,"//*[text()='通讯录']"
    #组织架构按钮
    __ORG_BTN=By.XPATH,"//*[text()='组织架构']"
    @allure.step("登录，进入企业微信web端首页")
    def login(self):
        '''
        登录，进入首页
        :return:
        '''
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame#/")

        time.sleep(3)

        # with open("../datas/cookie.yaml", "r", encoding="utf-8") as f:
        #     cookies = yaml.safe_load(f)
        # print(f"读出来的cookie{cookies}")
        # for cookie in cookies:
        #     try:
        #         self.driver.add_cookie(cookie)
        #     except Exception as e:
        #         print(e)
        # time.sleep(3)
        # self.driver.refresh()
        self.login_by_cookie()
        return self

    @allure.step("点击通讯录按钮，点击组织架构按钮，跳转至通讯录页面")
    def goto_contact_page(self):
        '''
        点击通讯录按钮，点击组织架构按钮，跳转至通讯录页面
        :return:
        '''
        self.click_ele(*self.__CONTACT_BTN)

        # self.driver.find_element(By.XPATH, "//*[text()='通讯录']")
        self.wait_ele_click(*self.__ORG_BTN)
        self.click_ele(*self.__ORG_BTN)
        # WebDriverWait(self.driver, 10).until(
        #     expected_conditions.element_to_be_clickable(
        #         (By.XPATH, "//*[text()='组织架构']")
        #     )
        # )

        return ContactPage(self.driver)